﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Windows;
    using PowerSong.SongDatabase;

    /// <summary>
    /// Interaction logic for AddCategoryWindow.xaml
    /// </summary>
    public partial class AddCategoryWindow : Window
        {

        public AddCategoryWindow()
            {
            InitializeComponent();
            var categoryCount = State.Database.Categories.Count();
            DeleteOtherCategoriesCheckBox.IsEnabled = categoryCount > 0;
            DefaultCheckBox.IsChecked = categoryCount == 0 ? true : false;
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;            
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a name has been specified
                if (string.IsNullOrEmpty(NameTextBox.Text))
                    {
                    throw new Exception("Please specify a name for the category.");
                    }

                // Ensure the name is unique
                if (State.Database.Categories.Contains(NameTextBox.Text))
                    {
                    throw new Exception("There already exists a category named '" + NameTextBox.Text + "'. Please specify a different name for this category.");
                    }

                // Create a category object
                var newCategory = new Category(NameTextBox.Text, DefaultCheckBox.IsChecked == true);

                // Delete all other categories if necessary
                var saveAllSongs = false;
                if (DeleteOtherCategoriesCheckBox.IsChecked == true)
                    {
                    foreach (var song in State.Database.Songs.GetAllSongs())
                        {
                        song.Categories.Clear();
                        }
                    foreach (var category in State.Database.Categories.GetAllCategories())
                        {
                        State.Database.Categories.Delete(category);
                        }
                    saveAllSongs = true;
                    }

                // Save the category to the database
                State.Database.Categories.Write(newCategory);

                // Assign all songs to the new category if necessary
                if (MoveAllSongsCheckBox.IsChecked == true)
                    {
                    foreach (var song in State.Database.Songs.GetAllSongs())
                        {
                        song.Categories.Add(newCategory);
                        }
                    saveAllSongs = true;
                    }

                // Do we need to update all songs?
                if (saveAllSongs)
                    {
                    foreach (var song in State.Database.Songs.GetAllSongs())
                        {
                        State.Database.Songs.Write(song);
                        }
                    }

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Cannot Create Category", ex.Message);
                }

            }

        private void DeleteOtherCategoriesCheckBox_Checked(object sender, RoutedEventArgs e)
            {
            MoveAllSongsCheckBox.IsChecked = true;
            }

        private void MoveAllSongsCheckBox_Unchecked(object sender, RoutedEventArgs e)
            {
            DeleteOtherCategoriesCheckBox.IsChecked = false;
            }

        }

    }
